// ------------------------------------------------------------------------------------------
// Licensed by Interprise Solutions.
// http://www.InterpriseSolutions.com
// For details on this license please visit  the product homepage at the URL above.
// THE ABOVE NOTICE MUST REMAIN INTACT.
// ------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Data.SqlClient;
using System.Data;
using InterpriseSuiteEcommerceCommon.Extensions;

namespace InterpriseSuiteEcommerceCommon.DTO
{
    [Serializable]
    public class SalesOrderHistory
    {
        #region Variable Declaration

        private Customer _thisCustomer = null;
        private int _row = 0;
        private string _salesOrderCode = string.Empty;
        private DateTime _salesOrderDate = DateTime.MinValue;
        private DateTime _shippingDate = DateTime.MinValue;
        private decimal _total = decimal.Zero;
        private string _notes = string.Empty;
        private DateTime _downloadEmailSentDate = DateTime.MinValue;
        private string _transactionState = string.Empty;        
        private string _paymentMethod = string.Empty;
        private int _electronicDownloadItemCount = 0;

        #endregion

        private SalesOrderHistory()
        {
        }

        public static SalesOrderHistory New(Customer thisCustomer, 
            int row, 
            string salesOrderCode, 
            DateTime salesOrderDate, 
            DateTime shippingDate,
            decimal total, 
            string notes, 
            string transactionState, 
            string paymentMethod,
            DateTime downloadEmailSentDate, 
            int downloadItemCount)//,
            //string trackingNumber)
        {
            SalesOrderHistory order = new SalesOrderHistory();
            order._thisCustomer = thisCustomer;
            order._row = row;
            order._salesOrderCode = salesOrderCode;
            order._salesOrderDate = salesOrderDate;
            order._shippingDate = shippingDate;
            order._total = total;
            order._notes = notes;
            order._transactionState = transactionState;
            order._paymentMethod = paymentMethod;
            order._downloadEmailSentDate = downloadEmailSentDate;
            order._electronicDownloadItemCount = downloadItemCount;
            
            return order;
        }

        [XmlIgnore()]
        public Customer ThisCustomer
        {
            get { return _thisCustomer; }
        }

        public int Row
        {
            get { return _row; }
            set { throw new NotSupportedException(); }
        }

        public string SalesOrderCode
        {
            get { return _salesOrderCode; }
            set { throw new NotSupportedException(); }
        }

        public DateTime SalesOrderDate
        {
            get { return _salesOrderDate; }
            set { throw new NotSupportedException(); }
        }

        public decimal Total
        {
            get { return _total; }
            set { throw new NotSupportedException(); }
        }

        public string Notes
        {
            get 
            {
                if (AppLogic.AppConfigBool("ShowCustomerServiceNotesInReceipts"))
                {
                    return CommonLogic.IIF(_notes.Length == 0, "None", _notes);
                }
                else
                {
                    return string.Empty;
                }
            }
            set { throw new NotSupportedException(); }
        }

        public DateTime ShippingDate
        {
            get { return _shippingDate; }
            set { throw new NotSupportedException(); }
        }

        public string PaymentStatus
        {
            get
            {
                string status = string.Format("{0} {1} <br /> {2}",
                                    AppLogic.GetString("account.aspx.17", ThisCustomer.SkinID, ThisCustomer.LocaleSetting),
                                    this._paymentMethod, 
                                    this._transactionState);

                return status;
            }
            set { throw new NotSupportedException(); }
        }

        public int ElectronicDownloadItemCount
        {
            get { return _electronicDownloadItemCount; }
            set { throw new NotSupportedException(); }
        }

        public string ShippingStatus
        {
            get 
            {
                string status = string.Empty;
                if (_shippingDate != DateTime.MinValue)
                {
                    status = string.Format("{0} {1} {2}",
                                AppLogic.GetString("account.aspx.18", ThisCustomer.SkinID, ThisCustomer.LocaleSetting),
                                AppLogic.GetString("account.aspx.19", ThisCustomer.SkinID, ThisCustomer.LocaleSetting),
                                _shippingDate.ToString("d", ThisCustomer.Culture));

                    using (SqlConnection con = DB.NewSqlConnection())
                    {
                        con.Open();
                        string sql = "SELECT AA.TrackingNumber,BB.ConfigValue + AA.TrackingNumber AS Link FROM Shipment AA with (NOLOCK) " +
                                     "LEFT JOIN ShipmentCarrier CC with (NOLOCK) ON AA.CarrierCode = CC.CarrierCode " +
                                     "LEFT JOIN ShippingAppConfig BB with (NOLOCK) ON AA.WarehouseCode = BB.WarehouseCode AND BB.Name = CC.CarrierDescription + '.OnlineTrackingURL'" +
                                     "WHERE AA.TrackingNumber <> '[To be generated]'" +
                                     "AND (AA.SourceDocument = " + DB.SQuote(_salesOrderCode) +
                                     " OR AA.SourceDocument = (SELECT InvoiceCode FROM CustomerInvoice with (NOLOCK) WHERE SourceInvoiceCode =" + DB.SQuote(_salesOrderCode) + "))";
                        using (IDataReader reader = DB.GetRSFormat(con, sql))
                        {
                            StringBuilder trackingNumbers = new StringBuilder();
                            string tracking = String.Empty;

                            while (reader.Read())
                            {

                                if (!DB.RSField(reader, "TrackingNumber").IsNullOrEmptyTrimmed())
                                {
                                    tracking = CommonLogic.IIF(DB.RSField(reader, "Link").IsNullOrEmptyTrimmed(), DB.RSField(reader, "TrackingNumber"), String.Format("<a href=\"{0}\" target=\"_blank\">{1}</a>", DB.RSField(reader, "Link"), DB.RSField(reader, "TrackingNumber")));
                                    trackingNumbers.AppendFormat("<br/>{0}", tracking);
                                }

                            }
                            if (trackingNumbers.Length > 0)
                            {
                                trackingNumbers = trackingNumbers.Insert(0, "<br/>" + AppLogic.GetString("account.aspx.32", ThisCustomer.SkinID, ThisCustomer.LocaleSetting));
                                status += trackingNumbers.ToString();
                            }
                        }
                        con.Close();
                    }
                }
                else
                {
                    status = AppLogic.GetString("account.aspx.20", ThisCustomer.SkinID, ThisCustomer.LocaleSetting);
                }

                if (_electronicDownloadItemCount > 0)
                {
                    if (_downloadEmailSentDate != DateTime.MinValue)
                    {
                        status +=   "<br />" + 
                                    string.Format(
                                        AppLogic.GetString("account.aspx.31", ThisCustomer.SkinID, ThisCustomer.LocaleSetting), 
                                        _downloadEmailSentDate.ToString("d", ThisCustomer.Culture));
                    }
                    else
                    {
                        status += "<br />" + AppLogic.GetString("account.aspx.21", ThisCustomer.SkinID, ThisCustomer.LocaleSetting);
                    }
                }

                return status;
            }
            set { throw new NotSupportedException(); }
        }
    }
}
